package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.util.SharingUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/23.
 */
public class PosSelectAdapter extends BaseAdapter{

    private Context context;
    private List<SharingUtil> list;
    private List<Integer> selectPos;


    public PosSelectAdapter(Context context,List<Integer> selectPos){

        this.context = context;

        this.selectPos = selectPos;
        this.list = new ArrayList<SharingUtil>();

        list.add(new SharingUtil(R.drawable.ic_launcher,"我的圈子"));
        list.add(new SharingUtil(R.drawable.ic_launcher,"全世界"));




    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {

        final ViewHolder holder;

        if(convertView == null){
            //新建一个holder
            convertView =  LayoutInflater.from(context).inflate(R.layout.dialog_select_pos_adapter_item, viewGroup, false);
            holder = new ViewHolder();

            holder.pic = (ImageView) convertView.findViewById(R.id.dialog_select_adapter_pic);
            holder.name = (TextView)convertView.findViewById(R.id.dialog_select_adapter_name);
            holder.checkBox = (CheckBox)convertView.findViewById(R.id.dialog_select_adapter_checkbox);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder)convertView.getTag();
        }

       holder.pic.setBackgroundResource(list.get(position).getIcon());
       holder.name.setText(list.get(position).getName());

        //判断分享到哪里且更换
       if(selectPos.contains(position))
           holder.checkBox.setChecked(true);
        else
           holder.checkBox.setChecked(false);
       holder.checkBox.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               if (holder.checkBox.isChecked()) {
                   // If the user checked the item, add it to the selected items
                   selectPos.add(position);
               } else if (selectPos.contains(position)) {
                   // Else, if the item is already in the array, remove it
                   selectPos.remove(Integer.valueOf(position));
               }

           }
       });



        return convertView;
    }



    class ViewHolder{

        ImageView pic;
        TextView name;
        CheckBox checkBox;


    }

}
